package com.junl.apps.service.xunchainfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junl.apps.common.PageInfoSetCondition;
import com.junl.apps.form.XunChaInfoForm;
import com.junl.apps.mapper.XunChaInfoMapper;
import com.junl.apps.model.XunChaInfoRelateModel;
import com.junl.apps.model.XunChaInfoXCModel;
import com.junl.apps.model.XunChaTouXCModel;
import com.junl.apps.service.role.RoleImpl;
import com.junl.apps.service.xunchatou.XunChaTouImpl;
import com.junl.frame.core.common.page.PageInfo;
import com.junl.frame.tools.UUIDGenerator;



@Service
public class XunChaInfoImpl implements IXunChaInfo{

	
	@Autowired
	private RoleImpl roleImpl;
	@Autowired
	private XunChaInfoMapper mapper;
	
	@Autowired
	private XunChaTouImpl xunchatouImpl;
	
	/*
	 * (non-Javadoc)
	 * @see com.junl.apps.service.xunchainfo.IXunChaInfo#queryXunChaInfoListPage(com.junl.apps.form.XunChaInfoForm)
	 */
	public PageInfo queryXunChaInfoListPage(XunChaInfoForm form) throws Exception {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("wxmkId", form.getWxmkId());
		params.put("sdate", form.getSdate());
		params.put("edate", form.getEdate());
		params.put("xunChaUserId", form.getXunChaUserId());
		params.put("notInTaskIds", form.getNotInTaskIds());
		params.put("xunChaTouId", form.getXunChaTouId());
		//设置分页参数
		PageInfoSetCondition.SetCondition(form.getPageInfo(), "xunChaTime", "desc");
		params.put("page", form.getPageInfo());
		//设置权限
		params.put("rolesql", roleImpl.getZHbyUserId(form.getXunChaUserId(),"info.xunChaStartZHK","info.xunChaStartZHM"));
		List<Map<String, Object>> list = mapper.queryXunChaInfoListPage(params);
		form.getPageInfo().setResults(list);
		
		return form.getPageInfo();
	}

	/**
	 *  根据巡查信息的ids获取维修项信息
	 */
	public List<Map<String, Object>> queryFzhlTable(String ids) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		return mapper.queryFzhlTable(params);
	}

	/**
	 * 新增
	 */
	public int insertXunInfo(XunChaInfoXCModel model) throws Exception {
		//设置主键
		model.setIds(UUIDGenerator.generate());
		//修改巡查登记的状态
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 1);
		map.put("ids",  model.getXunChaTouId());
		xunchatouImpl.updateXunChaTouState(map);
		for (int i = 0; i < model.getList().size(); i++) {
			//部件新增
			XunChaInfoRelateModel relate=model.getList().get(i);
			insertXunInfoFzhlRelate(relate);
		}
		return mapper.insertXunInfo(model);
	}

	/**
	 * 修改
	 */
	public int updateXunChaInfo(XunChaInfoXCModel model) throws Exception {
		//删除之前的部件
		deleteXunInfoFzhlRelateByXunChaInfoId(model.getIds());
		//新增现在所关联的部件
		for (int i = 0; i < model.getList().size(); i++) {
			//部件新增
			XunChaInfoRelateModel relate=model.getList().get(i);
			relate.setXunChaInfoId(model.getIds());
			insertXunInfoFzhlRelate(relate);
		}
		return mapper.updateXunChaInfo(model);
	}

	/**
	 * 部件新增
	 */
	public int insertXunInfoFzhlRelate(XunChaInfoRelateModel model) throws Exception {
		//设置主键
		model.setIds(UUIDGenerator.generate());
		return mapper.insertXunInfoFzhlRelate(model);
	}

	/**
	 * 根据巡查信息的ids删除部件
	 */
	public int deleteXunInfoFzhlRelateByXunChaInfoId(String xunChaInfoId) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("xunChaInfoId", xunChaInfoId);
		return mapper.deleteXunInfoFzhlRelateByXunChaInfoId(params);
	}
	
	

}
